/* GUI CONVERTED from gpmpupa.p (converter v1.76) Thu May  8 16:02:50 2003 */
/* gpmpupa.p - RECALCULATE MATERIALS PLAN - Update low level codes      */
/* Copyright 1986-2003 QAD Inc., Carpinteria, CA, USA.                  */
/* All rights reserved worldwide.  This is an unpublished work.         */
/* REVISION: 7.2       LAST EDIT: 01/17/95      MODIFIED BY: emb *F0GM*      */
/* REVISION: 7.2       LAST EDIT: 05/31/96      MODIFIED BY: emb *G1WY*      */
/* REVISION: 8.6E     LAST MODIFIED: 02/23/98   BY: *L007* A. Rahane */
/* REVISION: 8.6E     LAST MODIFIED: 03/12/98   BY: *J23R* Sandesh Mahagaokar */
/* REVISION: 8.6E     LAST MODIFIED: 10/04/98   BY: *J314* Alfred Tan         */
/* REVISION: 9.0      LAST MODIFIED: 04/06/00   BY: *M0LL* Kirti Desai        */
/* REVISION: 9.1      LAST MODIFIED: 07/19/00   BY: *N0GB* Rajinder Kamra     */
/* REVISION: 9.1      LAST MODIFIED: 08/23/00   BY: *N0ND* Mark Brown         */
/* $Revision: 1.8.1.5 $    BY: Patrick Rowan         DATE: 12/19/02  ECO: *P0M4*  */

/*V8:ConvertMode=Maintenance                                            */
/******************************************************************************/
/* All patch markers and commented out code have been removed from the source */
/* code below. For all future modifications to this file, any code which is   */
/* no longer required should be deleted and no in-line patch markers should   */
/* be added.  The ECO marker should only be included in the Revision History. */
/******************************************************************************/

     /*******************************************************************
     The purpose of this program is to register this process
     as doing a low-level code update, and to permit only one user
     process at a time do the update. When (if) our low-level code update
     program gets restructured so as to allow multiple concurrent
     processes to update low level codes, then the single-threaded
     code logic could be modified or removed, leaving only the
     registration code in place (to facilitate the multi-threaded
     code logic elsewhere in the MRP/DRP calculation).
********************************************************************/

define input parameter sync_code as character no-undo.
define shared variable mfguser as character.

define variable qad-recno as recid no-undo.
define variable qadmrpkey as character initial "mrp/drp" no-undo.
define variable dblcol as character initial "::" no-undo.
define buffer qadwkfl for qad_wkfl.
define variable msg01 as character no-undo.

define variable required-only like mfc_logical initial yes no-undo.

{mfglobal.i}

do transaction on error undo, retry:

   /******************************************************
Exclusive-lock (or create) this procedure's own MRP/DRP
   registration record.
   *******************************************************/

   find first qad_wkfl exclusive-lock where
      qad_key3 = qadmrpkey and qad_key4 = mfguser no-error.

   if not available qad_wkfl then
   find qad_wkfl exclusive-lock where qad_key1 = qadmrpkey
      and qad_key2 = mfguser + dblcol + qadmrpkey no-error.

   if not available qad_wkfl then create qad_wkfl.

   assign qad_key1 = qadmrpkey
      qad_key2 = mfguser + dblcol + qadmrpkey
      qad_key3 = qadmrpkey
      qad_key4 = mfguser
      qad_key5 = qadmrpkey + sync_code

      qad_key6 = "-99999"
      qad_charfld[1] = global_userid
      qad_datefld[1] = today
      qad_decfld[1] = time.

   /*************************************************************
   Note: low-level code of -99999 used above will prevent other
   synchronized MRP/DRP calculations from continuuing onto their
   next items while this low-level code process is working.
   **************************************************************/

   if recid(qad_wkfl) = -1 then. /* for Oracle */
end.

/*********************************************************
Look to see if any other processes are registered as doing
the low-level code update (qad_key2 = "lowlevel").
**********************************************************/
lowlevel:
repeat transaction on error undo, retry:

   qad-recno = ?.

   if retry then hide message no-pause.

   for first qadwkfl
      fields (qad_charfld qad_datefld qad_decfld qad_key1 qad_key2
      qad_key3 qad_key4 qad_key5 qad_key6)
      where qadwkfl.qad_key1 = qadmrpkey
        and qadwkfl.qad_key2 = "lowlevel"
      no-lock:
   end.

   if not available qadwkfl then do:

      pause 0 no-message.

      /************************************************************
      No other processes appear to be doing a low-level code update
      so register this user process for others to see.
      *************************************************************/
      qad_wkfl.qad_key2 = "lowlevel".

      next lowlevel.
   end.

   if available qadwkfl then do:

      qad-recno = recid(qadwkfl).

      /* WE FOUND OUR OWN RECORD, SO OKAY TO CONTINUE */
      if qadwkfl.qad_key4 = mfguser then leave.

      repeat:

         for first qadwkfl
            fields (qad_charfld qad_datefld qad_decfld qad_key1 qad_key2
            qad_key3 qad_key4 qad_key5 qad_key6)
            where recid(qadwkfl) = qad-recno
            no-lock:
         end.
         if not available qadwkfl then leave.
         if qadwkfl.qad_key2 <> "lowlevel" then leave.

         /***************************************************
         Attempt to delete the record we're waiting for,
         in case the process which created it has terminated
         (it's not really processing anymore) and no longer
         holds a share-lock on it.
         ****************************************************/

         if dbtype("qaddb") = "PROGRESS" then do:
            find qadwkfl exclusive-lock
               where recid(qadwkfl) = qad-recno no-wait no-error.
            if available qadwkfl then do:
               delete qadwkfl.
               next lowlevel.
            end.
         end.

         if msg01 = "" then do:

            {pxmsg.i &MSGNUM = 973 &MSGBUFFER = msg01}
            end.

         repeat:
            hide message no-pause.

            {pxmsg.i
               &MSGTEXT = msg01
               }
            pause 30.
            leave.
         end.
      end.
   end.
end.

/* Update the low level codes (inmstr.in_level) */

{gprun.i ""mrllup.p"" "(input required-only,
                        input true)"}

/******************************************************
Let other processes know that we're no longer doing the
low level code update process by changing qad_key2.
*******************************************************/
do transaction:
   find qad_wkfl exclusive-lock where recid(qad_wkfl) = qad-recno.

   assign qad_wkfl.qad_charfld[1] = global_userid
      qad_wkfl.qad_datefld[1] = today
      qad_wkfl.qad_decfld[1] = time
      qad_wkfl.qad_key2 = mfguser + dblcol + qadmrpkey.

end.
